home *** CD-ROM | disk | FTP | other *** search
/ Clickx 53 / Clickx 53.iso / software / onmisbaretool / firefoxv301 / Firefox Setup 3.0.1.exe / nonlocalized / modules / distribution.js < prev    next >
Encoding:
Text File  |  2008-07-02  |  11.0 KB  |  332 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the Firefox Distribution Customizations.
  15.  *
  16.  * The Initial Developer of the Original Code is Mozilla Foundation.
  17.  * Portions created by the Initial Developer are Copyright (C) 2007
  18.  * the Initial Developer. All Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  *   Dan Mills <thunder@mozilla.com>
  22.  *
  23.  * Alternatively, the contents of this file may be used under the terms of
  24.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  25.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  26.  * in which case the provisions of the GPL or the LGPL are applicable instead
  27.  * of those above. If you wish to allow use of your version of this file only
  28.  * under the terms of either the GPL or the LGPL, and not to allow others to
  29.  * use your version of this file under the terms of the MPL, indicate your
  30.  * decision by deleting the provisions above and replace them with the notice
  31.  * and other provisions required by the GPL or the LGPL. If you do not delete
  32.  * the provisions above, a recipient may use your version of this file under
  33.  * the terms of any one of the MPL, the GPL or the LGPL.
  34.  *
  35.  * ***** END LICENSE BLOCK ***** */
  36.  
  37. EXPORTED_SYMBOLS = [ "DistributionCustomizer" ];
  38.  
  39. const Ci = Components.interfaces;
  40. const Cc = Components.classes;
  41. const Cr = Components.results;
  42. const Cu = Components.utils;
  43.  
  44. function DistributionCustomizer() {
  45.   this._distroDir = this._dirSvc.get("XCurProcD", Ci.nsIFile);
  46.   this._distroDir.append("distribution");
  47.  
  48.   let iniFile = this._distroDir.clone();
  49.   iniFile.append("distribution.ini");
  50.   this._iniExists = iniFile.exists();
  51.  
  52.   if (!this._iniExists)
  53.     return;
  54.  
  55.   this._ini = Cc["@mozilla.org/xpcom/ini-parser-factory;1"].
  56.     getService(Ci.nsIINIParserFactory).createINIParser(iniFile);
  57.  
  58.   this._prefs = this._prefSvc.getBranch(null);
  59.   this._locale = this._prefs.getCharPref("general.useragent.locale");
  60.  
  61. }
  62. DistributionCustomizer.prototype = {
  63.   __bmSvc: null,
  64.   get _bmSvc() {
  65.     if (!this.__bmSvc)
  66.       this.__bmSvc = Cc["@mozilla.org/browser/nav-bookmarks-service;1"].
  67.                    getService(Ci.nsINavBookmarksService);
  68.     return this.__bmSvc;
  69.   },
  70.  
  71.   __annoSvc: null,
  72.   get _annoSvc() {
  73.     if (!this.__annoSvc)
  74.       this.__annoSvc = Cc["@mozilla.org/browser/annotation-service;1"].
  75.                    getService(Ci.nsIAnnotationService);
  76.     return this.__annoSvc;
  77.   },
  78.  
  79.   __dirSvc: null,
  80.   get _dirSvc() {
  81.     if (!this.__dirSvc)
  82.       this.__dirSvc = Cc["@mozilla.org/file/directory_service;1"].
  83.         getService(Ci.nsIProperties);
  84.     return this.__dirSvc;
  85.   },
  86.  
  87.   __prefSvc: null,
  88.   get _prefSvc() {
  89.     if (!this.__prefSvc)
  90.       this.__prefSvc = Cc["@mozilla.org/preferences-service;1"].
  91.         getService(Ci.nsIPrefService);
  92.     return this.__prefSvc;
  93.   },
  94.  
  95.   __iosvc: null,
  96.   get _iosvc() {
  97.     if (!this.__iosvc)
  98.       this.__iosvc = Cc["@mozilla.org/network/io-service;1"].
  99.                    getService(Ci.nsIIOService);
  100.     return this.__iosvc;
  101.   },
  102.  
  103.   _locale: "en-US",
  104.   _distroDir: null,
  105.   _iniExists: false,
  106.   _ini: null,
  107.  
  108.  
  109.   _makeURI: function DIST__makeURI(spec) {
  110.     return this._iosvc.newURI(spec, null, null);
  111.   },
  112.   _parseBookmarksSection: function DIST_parseBookmarksSection(parentId, section) {
  113.     let keys = [];
  114.     for (let i in enumerate(this._ini.getKeys(section)))
  115.       keys.push(i);
  116.     keys.sort();
  117.     let items = {};
  118.     let defaultItemId = -1;
  119.     let maxItemId = -1;
  120.  
  121.     for (let i = 0; i < keys.length; i++) {
  122.       let m = /^item\.(\d+)\.(\w+)\.?(\w*)/.exec(keys[i]);
  123.       if (m) {
  124.         let [foo, iid, iprop, ilocale] = m;
  125.  
  126.         if (ilocale)
  127.           continue;
  128.  
  129.         if (!items[iid])
  130.           items[iid] = {};
  131.         if (keys.indexOf(keys[i] + "." + this._locale) >= 0) {
  132.           items[iid][iprop] = this._ini.getString(section, keys[i] + "." +
  133.                                                   this._locale);
  134.         } else {
  135.           items[iid][iprop] = this._ini.getString(section, keys[i]);
  136.         }
  137.  
  138.         if (iprop == "type" && items[iid]["type"] == "default")
  139.           defaultItemId = iid;
  140.  
  141.         if (maxItemId < iid)
  142.           maxItemId = iid;
  143.       } else {
  144.         dump("Key did not match: " + keys[i] + "\n");
  145.       }
  146.     }
  147.  
  148.     let prependIndex = 0;
  149.     for (let iid = 0; iid <= maxItemId; iid++) {
  150.       if (!items[iid])
  151.         continue;
  152.  
  153.       let index = -1;
  154.       let newId;
  155.  
  156.       switch (items[iid]["type"]) {
  157.       case "default":
  158.         break;
  159.  
  160.       case "folder":
  161.         if (iid < defaultItemId)
  162.           index = prependIndex++;
  163.  
  164.         newId = this._bmSvc.createFolder(parentId, items[iid]["title"], index);
  165.  
  166.         this._parseBookmarksSection(newId, "BookmarksFolder-" +
  167.                                     items[iid]["folderId"]);
  168.  
  169.         if (items[iid]["description"])
  170.           this._annoSvc.setItemAnnotation(newId, "bookmarkProperties/description",
  171.                                           items[iid]["description"], 0,
  172.                                           this._annoSvc.EXPIRE_NEVER);
  173.  
  174.         break;
  175.  
  176.       case "separator":
  177.         if (iid < defaultItemId)
  178.           index = prependIndex++;
  179.         this._bmSvc.insertSeparator(parentId, index);
  180.         break;
  181.  
  182.       case "bookmark":
  183.       default:
  184.         if (iid < defaultItemId)
  185.           index = prependIndex++;
  186.  
  187.         newId = this._bmSvc.insertBookmark(parentId,
  188.                                            this._makeURI(items[iid]["link"]),
  189.                                            index, items[iid]["title"]);
  190.  
  191.         if (items[iid]["description"])
  192.           this._annoSvc.setItemAnnotation(newId, "bookmarkProperties/description",
  193.                                           items[iid]["description"], 0,
  194.                                           this._annoSvc.EXPIRE_NEVER);
  195.  
  196.         break;
  197.       }
  198.     }
  199.   },
  200.   applyCustomizations: function DIST_applyCustomizations() {
  201.     if (!this._iniExists)
  202.       return;
  203.  
  204.     // nsPrefService loads very early.  Reload prefs so we can set
  205.     // distribution defaults during the prefservice:after-app-defaults
  206.     // notification (see applyPrefDefaults below)
  207.     this._prefSvc.QueryInterface(Ci.nsIObserver);
  208.     this._prefSvc.observe(null, "reload-default-prefs", null);
  209.  
  210.     let sections = enumToObject(this._ini.getSections());
  211.  
  212.     // The global section, and several of its fields, is required
  213.     // (we also check here to be consistent with applyPrefDefaults below)
  214.     if (!sections["Global"])
  215.       return;
  216.     let globalPrefs = enumToObject(this._ini.getKeys("Global"));
  217.     if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
  218.       return;
  219.  
  220.     let bmProcessed = false;
  221.     let bmProcessedPref = "distribution." +
  222.       this._ini.getString("Global", "id") + ".bookmarksProcessed";
  223.     try {
  224.       bmProcessed = this._prefs.getBoolPref(bmProcessedPref);
  225.     } catch (e) {}
  226.  
  227.     if (!bmProcessed) {
  228.       if (sections["BookmarksMenu"])
  229.         this._parseBookmarksSection(this._bmSvc.bookmarksRoot,
  230.                                     "BookmarksMenu");
  231.       if (sections["BookmarksToolbar"])
  232.         this._parseBookmarksSection(this._bmSvc.toolbarFolder,
  233.                                     "BookmarksToolbar");
  234.       this._prefs.setBoolPref(bmProcessedPref, true);
  235.     }
  236.   },
  237.   applyPrefDefaults: function DIST_applyPrefDefaults() {
  238.     if (!this._iniExists)
  239.       return;
  240.  
  241.     let sections = enumToObject(this._ini.getSections());
  242.  
  243.     // The global section, and several of its fields, is required
  244.     if (!sections["Global"])
  245.       return;
  246.     let globalPrefs = enumToObject(this._ini.getKeys("Global"));
  247.     if (!(globalPrefs["id"] && globalPrefs["version"] && globalPrefs["about"]))
  248.       return;
  249.  
  250.     let defaults = this._prefSvc.getDefaultBranch(null);
  251.  
  252.     // Global really contains info we set as prefs.  They're only
  253.     // separate because they are "special" (read: required)
  254.  
  255.     defaults.setCharPref("distribution.id", this._ini.getString("Global", "id"));
  256.     defaults.setCharPref("distribution.version",
  257.                          this._ini.getString("Global", "version"));
  258.  
  259.     let partnerAbout = Cc["@mozilla.org/supports-string;1"].
  260.       createInstance(Ci.nsISupportsString);
  261.     if (globalPrefs["about." + this._locale]) {
  262.       partnerAbout.data = this._ini.getString("Global", "about." + this._locale);
  263.     } else {
  264.       partnerAbout.data = this._ini.getString("Global", "about");
  265.     }
  266.     defaults.setComplexValue("distribution.about",
  267.                              Ci.nsISupportsString, partnerAbout);
  268.  
  269.     if (sections["Preferences"]) {
  270.       for (let key in enumerate(this._ini.getKeys("Preferences"))) {
  271.         try {
  272.           let value = eval(this._ini.getString("Preferences", key));
  273.           switch (typeof value) {
  274.           case "bool":
  275.             defaults.setBoolPref(key, value);
  276.             break;
  277.           case "int":
  278.             defaults.setIntPref(key, value);
  279.             break;
  280.           case "string":
  281.             defaults.setCharPref(key, value);
  282.             break;
  283.           case "undefined":
  284.             defaults.setCharPref(key, value);
  285.             break;
  286.           }
  287.         } catch (e) { /* ignore bad prefs and move on */ }
  288.       }
  289.     }
  290.  
  291.     // We eval() the localizable prefs as well (even though they'll
  292.     // always get set as a string) to keep the INI format consistent:
  293.     // string prefs always need to be in quotes
  294.  
  295.     let localizedStr = Cc["@mozilla.org/pref-localizedstring;1"].
  296.       createInstance(Ci.nsIPrefLocalizedString);
  297.  
  298.     if (sections["LocalizablePreferences"]) {
  299.       for (let key in enumerate(this._ini.getKeys("LocalizablePreferences"))) {
  300.         try {
  301.           let value = eval(this._ini.getString("LocalizablePreferences", key));
  302.           value = value.replace("%LOCALE%", this._locale, "g");
  303.           localizedStr.data = "data:text/plain," + key + "=" + value;
  304.           defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
  305.         } catch (e) { /* ignore bad prefs and move on */ }
  306.       }
  307.     }
  308.  
  309.     if (sections["LocalizablePreferences-" + this._locale]) {
  310.       for (let key in enumerate(this._ini.getKeys("LocalizablePreferences-" + this._locale))) {
  311.         try {
  312.           let value = eval(this._ini.getString("LocalizablePreferences-" + this._locale, key));
  313.           localizedStr.data = "data:text/plain," + key + "=" + value;
  314.           defaults.setComplexValue(key, Ci.nsIPrefLocalizedString, localizedStr);
  315.         } catch (e) { /* ignore bad prefs and move on */ }
  316.       }
  317.     }
  318.   }
  319. };
  320.  
  321. function enumerate(UTF8Enumerator) {
  322.   while (UTF8Enumerator.hasMore())
  323.     yield UTF8Enumerator.getNext();
  324. }
  325.  
  326. function enumToObject(UTF8Enumerator) {
  327.   let ret = {};
  328.   for (let i in enumerate(UTF8Enumerator))
  329.     ret[i] = 1;
  330.   return ret;
  331. }
  332.